/*
The MIT License (MIT)
Copyright (c) 2013 Dalvik Boss dalvikboss@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package sidl.generator.csharp;

import sidl.generator.Generator;
import sidl.generator.GeneratorContext;

public class CSharpByteBufferGenerator extends Generator {

	public CSharpByteBufferGenerator(GeneratorContext context) {
		super(context);
	}

	@Override
	protected void onGenerate() throws Throwable {
        println("public class ByteBuffer");
        println("{");
        println("");
        println("    private byte[] data = null;");
        println("    private int capcity = 0;");
        println("    private int pos = 0;");
        println("    public static ByteBuffer allocate(int capcity)");
        println("    {");
        println("        ByteBuffer bb = new ByteBuffer();");
        println("        bb.data = new byte[capcity];");
        println("        bb.capcity = capcity;");
        println("        bb.pos = 0;");
        println("        return bb;");
        println("    }");
        println("");
        println("");
        println("");
        println("    public void putDouble(double value)");
        println("    {");
        println("        put(BitConverter.GetBytes(value));");
        println("    }");
        println("");
        println("    public void putFloat(float value)");
        println("    {");
        println("");
        println("        put(System.BitConverter.GetBytes(value));");
        println("    }");
        println("");
        println("");
        println("    public void putLong(long value)");
        println("    {");
        println("        data[pos++] = (byte)(value >> 56 & 0xff);");
        println("        data[pos++] = (byte)(value >> 48 & 0xff);");
        println("        data[pos++] = (byte)(value >> 40 & 0xff);");
        println("        data[pos++] = (byte)(value >> 32 & 0xff);");
        println("        data[pos++] = (byte)(value >> 24 & 0xff);");
        println("        data[pos++] = (byte)(value >> 16 & 0xff);");
        println("        data[pos++] = (byte)(value >> 8 & 0xff);");
        println("        data[pos++] = (byte)(value >> 0 & 0xff);");
        println("    }");
        println("");
        println("");
        println("    public void putInt(int index, int value)");
        println("    {");
        println("        data[index + 3] = (byte)(value >> 0 & 0xff);");
        println("        data[index + 2] = (byte)(value >> 8 & 0xff);");
        println("        data[index + 1] = (byte)(value >> 16 & 0xff);");
        println("        data[index + 0] = (byte)(value >> 24 & 0xff);");
        println("    }");
        println("");
        println("    public void putInt(int value)");
        println("    {");
        println("        data[pos++] = (byte)(value >> 24 & 0xff);");
        println("        data[pos++] = (byte)(value >> 16 & 0xff);");
        println("        data[pos++] = (byte)(value >> 8 & 0xff);");   
        println("        data[pos++] = (byte)(value >> 0 & 0xff);");
        println("    }");
        println("    public void putShort(short value)");
        println("    {");
        println("        data[pos++] = (byte)(value >> 8 & 0xff);");
        println("        data[pos++] = (byte)(value >> 0 & 0xff);");
        println("    }");
        println("");
        println("");
        println("");
        println("    public void putChar(char c)");
        println("    {");
        println("        data[pos++] = (byte)c;");
        println("");
        println("    }");
        println("");
        println("    public void put(byte b)");
        println("    {");
        println("        data[pos++] = b;");
        println("");
        println("    }");
        println("    public void put(byte[] bytes)");
        println("    {");
        println("        Array.Copy(bytes, 0, data, pos, bytes.Length);");
        println("        pos += bytes.Length;");
        println("    }");
        println("");
        println("    public int getInt()");
        println("    {");
        println("        int i =");
        println("            (int)((data[pos++] << 24) & 0xff000000) |");
        println("            (int)((data[pos++] << 16) & 0x00ff0000) |");
        println("            (int)((data[pos++] << 8) & 0x0000ff00) |");
        println("            (int)((data[pos++] << 0 ) & 0x000000ff) ;");
        println("        return i;");
        println("    }");
        println("");
        println("    public char getChar()");
        println("    {");
        println("        return (char)data[pos++];");
        println("    }");
        println("    public short getShort()");
        println("    {");
        println("        short i = (short)((((data[pos++] << 8) & 0xff00)) | ((data[pos++] << 0) & 0x00ff)) ;");
        println("        return i;");
        println("    }");
        println("    public double getDouble()");
        println("    {");
        println("        double d = BitConverter.ToDouble(data, pos);");
        println("        pos += 8;");
        println("        return d;");
        println("");
        println("    }");
        println("");
        println("");
        println("    public float getFloat()");
        println("    {");
        println("        float f = BitConverter.ToSingle(data, pos);");
        println("        pos += 4;");
        println("        return f;");
        println("");
        println("    }");
        println("");
        println("");
        println("    public byte get()");
        println("    {");
        println("        return data[pos++];");
        println("    }");
        println("");
        println("    public void get(byte[] bytes)");
        println("    {");
        println("        Array.Copy(data, pos, bytes, 0, bytes.Length);");
        println("        pos += bytes.Length;");
        println("    }");
        println("");
        println("");
        println("");
        println("    public long getLong()");
        println("    {");
        println("        unchecked");
        println("        {");
        println("            long i =");
        println("                (long)((long)(data[pos++] << 56) & (long)0xff00000000000000L) |");
        println("                (long)((long)(data[pos++] << 48) & (long)0x00ff000000000000L) |");
        println("                (long)((long)(data[pos++] << 40) & (long)0x0000ff0000000000L) |");
        println("                (long)((long)(data[pos++] << 32) & (long)0x000000ff00000000L) |");
        println("                (long)((long)(data[pos++] << 24) & (long)0x00000000ff000000L) |");
        println("                (long)((long)(data[pos++] << 16) & (long)0x0000000000ff0000L) |");
        println("                (long)((long)(data[pos++] << 8)  & (long)0x000000000000ff00L) |");
        println("                (long)((long)(data[pos++] << 0)  & (long)0x00000000000000ffL) ;");
        println("");
        println("            return i;");
        println("        }");
        println("");
        println("    }");
        println("");
        println("    public void clear()");
        println("    {");
        println("        pos = 0;");
        println("    }");
        println("    public void position(int pos)");
        println("    {");
        println("        this.pos = pos;");
        println("    }");
        println("");
        println("    public int position()");
        println("    {");
        println("        return pos;");
        println("    }");
        println("");
        println("    public byte[] array()");
        println("    {");
        println("        return data;");
        println("    }");
        println("");
        println("");
        println("}");

	}

}
